package auctionhaus

import org.springframework.transaction.annotation.Transactional;


@Transactional
class CrudService {

	/**
	 SRV-1: Create a Grails service method that supports creating a new customer (unit test)
	 */

	boolean createCustomer(Customer customer){
		if(!customer.validate()){
			return false
		}
		return customer.save(flush:true) != null;
	}
	
	/**
	SRV-2: Create a Grails service method that supports creating a new listing (unit test)
	*/

	boolean createListing(Listing listing){
		if(!listing.validate()){
			return false
		}
		return listing.save(flush:true) != null;
	}
	
	/**
	SRV-3: Create a Grails service method that supports creating a new bid for a listing (unit test)
	*/

	boolean createBid(Bid bid){
		Date now = new Date()
		if(!bid.validate() || now.after(bid.listing.endDateTime)){
			return false
		}
		return bid.save(flush:true) != null;
	}

	boolean createRole(Role role){
		if(!role.validate()){
			return false
		}
		return role.save(flush:true) != null;
	}

	boolean updateCustomer(Customer customer){
		if(!customer.validate()){
			return false
		}
		return customer.save(flush:true) != null;
	}

	boolean updateListing(Listing listing){
		if(!listing.validate()){
			return false
		}
		return listing.save(flush:true) != null;
	}

	boolean updateBid(Bid bid){
		if(!bid.validate()){
			return false
		}
		return bid.save(flush:true) != null;
	}

	boolean updateRole(Role role){
		if(!role.validate()){
			return false
		}
		return role.save(flush:true) != null;
	}

	boolean deleteCustomer(Customer customer){
		customer.delete(flush:true)
	}

	void deleteListing(Listing listing){
		listing.delete(flush:true)
	}

	boolean deleteBid(Bid bid){
		bid.delete(flush:true)
	}

	boolean deleteRole(Role role){
		role.delete(flush:true)
	}
}
